<?php
require("libraries/agency.php");
class Album extends Agency{
	protected $_user = "";
	public function __construct(){
		parent::__construct();
		$this->load->helper("form");
		$this->load->model("model_cruises");
		$this->_user = $this->session->userdata('ses_userid');
	}
	public function index(){
		$albid = $this->uri->segment(4);
		if($albid == NULL){ die("Page not found");}
		$data['listimg'] = $this->model_cruises->gallery($albid);
		$data['data'] = "";$data['act'] = 3;
		$data['albumid'] = $albid;
		$data['title'] = "Album cruise";
		$this->load->view("album/list",$data);
	}
	public function doupload(){
		if(!isset($_POST['okie'])){ die();}
		$albid = $_POST['album'];
		if($_FILES['file']['name'] != NULL){
			if($_FILES['file']['type'] == "image/jpeg"
				  || $_FILES['file']['type'] == "image/png"
				  || $_FILES['file']['type'] == "image/gif"){
				 if($_FILES['file']['size'] < (5*1048576)){
					 move_uploaded_file($_FILES['file']['tmp_name'],"uploads/albums/gallerys/".$_FILES['file']['name']);
					 $this->createThumbs($_FILES['file']['name'],"albums/gallerys","130","90");
					 $filename = $_FILES['file']['name'];
					 $db = array("gallery_name"=> $filename,"gallery_image"=>$filename,"album_id"=>$albid);
					 $this->model_cruises->addgal($db);
					 redirect(base_url()."agency/album/index/".$albid);
				 }else{
					 echo "File no larger than 5mb";
				 }
			}else{
				  echo "Invalid file type (Include jpg, png, gif)";
			}
			
		}else{
			echo "Please select a file";
		}
		//move_uploaded_file($_FILES['file']['tmp_name'],"uploads/albums/gallerys".$_FILES['file']['name']);
	}
	public function del(){
		$id = $_POST['id'];
		$data = $this->model_cruises->getgalery($id);
		@unlink("uploads/albums/gallerys/thumb/".$data['gallery_image']);
		@unlink("uploads/albums/gallerys/".$data['gallery_image']);
		$this->model_cruises->delgalery($id);
	}
	public function createThumbs($fileName,$path,$width,$height){
		$this->load->library('image_lib');
		$config['image_library'] = 'gd2';
		$config['source_image'] = 'uploads/'.$path.'/'.$fileName;
		$config['new_image']    = 'uploads/'.$path.'/thumb/'.$fileName;
		$config['create_thumb'] = TRUE;
		//$config['maintain_ratio'] = TRUE;
		$config['thumb_marker'] = FALSE;
		//$config['maintain_ratio'] = FALSE;
		$config['width'] = $width;
		$config['height'] = $height;
		$this->image_lib->initialize($config); 
		$this->image_lib->resize();
		$this->image_lib->clear();
	}
}